package com.lemon.smartsafetynew.ui.activity.hidden;

import android.os.Bundle;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.activity.base.PhotoSelectBaseTwoActivity;
import com.lemon.smartsafety.ui.adapter.common.SelectEvidenceAdapter;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenProcessItemVo;
import com.lemon.smartsafetynew.net.services.SafeHiddenServices;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import butterknife.OnClick;

/**
 * 上传文件图片
 * Created by Andrew on 2017/11/2.
 */

public class SafeHiddenUploadDocumentPictureActivity extends PhotoSelectBaseTwoActivity {


    private static String docType = "";
    private static SafeHiddenProcessItemVo safeHiddenProcessItemVo;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {

            if (bundle.containsKey(KeyConstants.KEY_DOC_TYPE)) {
                docType = bundle.getString(KeyConstants.KEY_DOC_TYPE);
            }

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_PROCESS_ITEM_VO)) {
                safeHiddenProcessItemVo = (SafeHiddenProcessItemVo) bundle.getSerializable(KeyConstants.KEY_SAFE_HIDDEN_PROCESS_ITEM_VO);
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle("上传文件图片");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(SafeHiddenUploadDocumentPictureActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_hidden_upload_document_picture;
    }

    @OnClick(R.id.bottom_button)
    public void onViewClicked() {

        mHUDUtil.showHUD();

        final LemonRequestParams params = new LemonRequestParams("");
        params.put("danger_id", safeHiddenProcessItemVo.getDanger_id());
        params.put("record_id", safeHiddenProcessItemVo.getId());

        ExecutorService fixedThreadPool = Executors.newSingleThreadExecutor();
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {

                getUploadMediaItems(new SelectEvidenceAdapter.CropCallBack<Map<String, File>>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onFinish(Map<String, File> data) {

                        if (data.isEmpty()) {

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mHUDUtil.showErrorTip("请添加上传的文书图片。");
                                }
                            });

                        } else {
                            SafeHiddenServices
                                    .init()
                                    .uploadDocPicture(params, data);
                        }
                    }
                });
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_UPLOAD_DOC_PICTURE_FILE.equals(event.getTag())) {                      //文书图片上传
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                //mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                mHUDUtil.hidden();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    callFinish();
                } else {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                }
            }
        }

    }
}
